package aom.ju.ss.view.base

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Message
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe


abstract class BaseActivity : AppCompatActivity(), MyHttpCallBack, View.OnClickListener {
    protected var viewBinding: ViewDataBinding? = null
    protected var mContext: Context? = null
    protected var activity: Activity? = null


    var controller: WindowInsetsControllerCompat? = null
    override fun ok(jsonString: String, httpTY: String) {}
    override fun error(e: String, jsonString: String, httpTY: String) {}
    override fun onClick(v: View) {}
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            decorView!!.systemUiVisibility =
//                (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                        or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
//        } else {
//            decorView!!.systemUiVisibility =
//                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//        }

        mContext = this
        activity = this
        if (lay != 0) {
            viewBinding = DataBindingUtil.setContentView(this, lay)
        }
        viewBinding?.root?.let {
            controller = ViewCompat.getWindowInsetsController(it)
            controller?.show(WindowInsetsCompat.Type.statusBars())//显示状态栏
        }

        if (Build.VERSION.SDK_INT >= 21) {
            val decorView = window.decorView
            decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.statusBarColor = Color.TRANSPARENT
        }
        controller?.isAppearanceLightStatusBars = true
        init()


    }

    protected abstract fun init()
    protected abstract val lay: Int

    @Subscribe
    open fun callBack(event: Message) {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
}